<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="orderReqDto.phone" placeholder="手机号" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="tableData"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="编号" align="center" width="80">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="内容" align="left" mini-width="150">
        <template slot-scope="{row}">
          <span>{{ row.content }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否已读" align="center" width="100">
        <template slot-scope="{row}">
          <el-tag v-if="row.checked ===0" type="primary">未读</el-tag>
          <el-tag v-if="row.checked ===1" type="success">已读</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="手机号" width="150" align="center">
        <template slot-scope="{row}">
          {{ row.phone }}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" width="160" align="center">
        <template slot-scope="{row}">
          <span>{{ row.gmtDatetime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="320" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="pull(row)">
            查看相关记录
          </el-button>
          <el-button type="info" size="mini" @click="check(row)">
            确认已读
          </el-button>
          <el-button type="warning" size="mini" @click="handlereply(row)">
            回复留言
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="orderReqDto.current"
      :limit.sync="orderReqDto.size"
      @pagination="getCustomerserivces"
    />
    <el-dialog title="客服记录" :visible.sync="dialogFormVisible" width="450px" height="300px">
      <div class="test1" style="height:500px;overflow-y:scroll;overflow-x:none">
        <div v-for="item in msgs" :key="item.id" class="traces">
          <div :class="item.waiterSender == 1 ?'right':'left'">
            <img :src="item.waiterSender == 1 ? 'https://yix-static.oss-cn-hangzhou.aliyuncs.com/yixinh5/waiter.png': 'https://yix-static.oss-cn-hangzhou.aliyuncs.com/yixinh5/user.png'" alt="" :class="item.waiterSender == 1 ?'rightimg':'leftimg'">
            <div :class="item.waiterSender == 1 ?'rightcontent':'leftcontent'" v-html="item.content" />
          </div>
        </div>
      </div>
    </el-dialog>

    <el-dialog title="客服回复" :visible.sync="editdialog">
      <el-form ref="dataForm" :rules="rules" :model="replyDto" label-position="left" label-width="90px" style="width: 400px; margin-left:50px;">
        <el-form-item label="回复内容">
          <textarea v-model="replyDto.replyContent" type="text" style="width:400px;height:200px" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel()">
          取消
        </el-button>
        <el-button type="primary" @click="reply()">
          回复
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getCustomerserivces, pull, check, reply } from '@/api/service'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
export default {
  name: 'Service',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      msgs: [],
      tableData: [],
      total: 0,
      tableKey: 0,
      orderReqDto: {
        size: 10,
        current: 1,
        all: true,
        phone: ''
      },
      countOrder: {},
      dialogFormVisible: false,
      editdialog: false,
      replyDto: {
        id: 0,
        replyContent: ''
      },
      dialogStatus: '',
      textMap: {
        update: '订单流水'
      },
      dialogPvVisible: false,
      pvData: [],
      rules: {
        type: [{ required: true, message: 'type is required', trigger: 'change' }],
        timestamp: [{ type: 'date', required: true, message: 'timestamp is required', trigger: 'change' }],
        username: [{ required: true, message: 'title is required', trigger: 'blur' }]
      },
      listLoading: false,
      orderNoForDetail: '',
      optionsCopy: [],
      options: [],
      value: ''
    }
  },
  created() {
    this.getCustomerserivces(this.orderReqDto)
  },
  methods: {
    dataFilter(val) {
      this.value = val
      if (val) { // val存在
        this.options = this.optionsCopy.filter((item) => {
          console.log(item)
          if (!!~item.channel_name.indexOf(val) || !!~item.channel_name.toUpperCase().indexOf(val.toUpperCase())) {
            return true
          }
        })
      } else { // val为空时，还原数组
        this.options = this.optionsCopy
      }
    },
    async pull(row) {
      const res = await pull({ id: row.id })
      this.msgs = res.data.data
      this.dialogFormVisible = true
    },
    // 获取订单列表
    async getCustomerserivces(orderReqDto) {
      const res = await getCustomerserivces(this.orderReqDto)
      if (res.data.data.records === null) {
        this.tableData = null
      } else {
        this.tableData = res.data.data.records
        this.total = res.data.data.total
      }
    },
    // 确认已读
    async check(row) {
      const res = await check({ id: row.id })
      if (res.data.code === 200) {
        this.getCustomerserivces()
        this.$notify({
          title: '确认已读！',
          message: '确认成功',
          type: 'success',
          duration: 2000
        })
      } else {
        this.$notify({
          title: '确认已读！',
          message: '确认失败',
          type: 'fail',
          duration: 2000
        })
      }
    },
    handleFilter() {
      this.orderReqDto.current = 1
      this.orderReqDto.all = false
      this.getCustomerserivces(this.orderReqDto)
    },
    cancel() {
      this.editdialog = false
    },
    handlereply(row) {
      this.replyDto.id = row.id
      this.replyDto.replyContent = ''
      this.editdialog = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    // 回复
    async reply() {
      const res = await reply(this.replyDto)
      if (res.data.code === 200) {
        this.$notify({
          title: '客服回复！',
          message: res.data.msg,
          type: 'success',
          duration: 2000
        })
        this.getCustomerserivces()
      } else {
        this.$notify({
          title: '客服回复！',
          message: res.data.msg,
          type: 'fail',
          duration: 2000
        })
      }
      this.editdialog = false
    }
  }
}
</script>

<style scoped>
.traces{
  overflow:hidden;
}
.left{
  overflow:hidden;
  margin-left: 15px;
  margin-top: 10px;
  width: 100%;
}
.leftcontent{
  word-wrap:break-word;
  padding-left: 10px;
  padding-right: 10px;
  margin-left: 10px;
  border-radius: 5px;
  color: #666666;
  background-color: #EBEBEB;
  width: 235px;
  float: left;
  text-align:left;
  line-height: 40px;
}
.divimg{
  width: 28px;
}
.leftimg{
  display: block;
  float: left;
  width: 38px;
  border-radius: 50%;
}
.rightimg{
  margin-right: 15px;
  display: block;
  float: right;
  width: 38px;
  border-radius: 50%;
}
.right{
  overflow:hidden;
  padding-right: 10px;
  margin-top: 10px;
  width: 100%;
}
.rightcontent{
  word-wrap:break-word;
  padding-left: 10px;
  padding-right: 10px;
  margin-right: 10px;
  border-radius: 5px;
  background-color: #F6AC02;
  color: white;
  width: 235px;
  float: right;
  text-align:left;
  line-height: 40px;
}
.test1::-webkit-scrollbar {
 width: 0;
}
 .test1::-webkit-scrollbar-track {
 background-color:white;
 -webkit-border-radius: 2em;
 -moz-border-radius: 2em;
 border-radius:2em;
}
 .test1::-webkit-scrollbar-thumb {
 background-color:white;
 -webkit-border-radius: 2em;
 -moz-border-radius: 2em;
 border-radius:2em;
}
</style>
